//
//  StatusCellTopView.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/7.
//  Copyright © 2015年 002. All rights reserved.
//

#import "StatusCellTopView.h"
#import "UIImageView+Extention.h"
#import "UILabel+Extention.h"
#import "masonry.h"
#import "UIImageView+WebCache.h"

@interface StatusCellTopView ()

// 用户头像
@property(nonatomic,strong) UIImageView *iconView;

// 姓名标签
@property(nonatomic,strong) UILabel *nameLabel;

// 会员图标
@property(nonatomic,strong) UIImageView *memberIconView;

// 认证图标
@property(nonatomic,strong) UIImageView *vipIconView;

// 时间标签
@property(nonatomic,strong) UILabel *timeLabel;

// 来源标签
@property(nonatomic,strong) UILabel *sourceLabel;

@end

@implementation StatusCellTopView

// 构造函数
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor blueColor];
        [self setupUI];
    }
    return self;
}

/**
*  设置UI
*/
- (void)setupUI {
    
    self.backgroundColor = [[UIColor alloc] initWithWhite:0.9 alpha:1.0];
    
    // 1. 添加控件
    [self addSubview:self.iconView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.memberIconView];
    [self addSubview:self.vipIconView];
    [self addSubview:self.timeLabel];
    [self addSubview:self.sourceLabel];
    
    // 2. 自动布局
    // 头像
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(StatusCellMagrin);
        make.left.equalTo(self.mas_left).offset(StatusCellMagrin);
        make.width.equalTo(StatusIconWidth);
        make.height.equalTo(StatusIconWidth);
    }];
    
    // 姓名标签
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_top);
        make.left.equalTo(self.iconView.mas_right).offset(StatusCellMagrin);
    }];
    
    // 会员标签
    [self.memberIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_top);
        make.left.equalTo(self.nameLabel.mas_right).offset(StatusCellMagrin);
    }];
    
    // 认证图标
    [self.vipIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_right);
        make.centerY.equalTo(self.iconView.mas_bottom);
    }];
    
    // 时间标签
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView.mas_bottom);
        make.left.equalTo(self.iconView.mas_right).offset(StatusCellMagrin);
    }];
    
    // 来源标签
    [self.sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.timeLabel.mas_bottom);
        make.left.equalTo(self.timeLabel.mas_right).offset(StatusCellMagrin);
    }];
}

#pragma mark - 懒加载
- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [UIImageView initWithImageName:@"avatar_default_big"];
    }
    return _iconView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel initWithTitle:@"我就是刀哥" fontSize:12 color:[UIColor darkGrayColor]];
    }
    return _nameLabel;
}

- (UIImageView *)memberIconView {
    if (!_memberIconView) {
        _memberIconView = [UIImageView initWithImageName:@"common_icon_membership_level1"];
    }
    return _memberIconView;
}

- (UIImageView *)vipIconView {
    if (!_vipIconView) {
        _vipIconView = [UIImageView initWithImageName:@"avatar_grassroot"];
    }
    return _vipIconView;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel initWithTitle:@"刚刚" fontSize:11 color:[UIColor orangeColor]];
    }
    return _timeLabel;
}

- (UILabel *)sourceLabel {
    if (!_sourceLabel) {
        _sourceLabel = [UILabel initWithTitle:@"黑马微博" fontSize:11 color:nil];
    }
    return _sourceLabel;
}

/**
 *  根据视图模型赋值
 *
 *  @param viewModel 微博视图模型
 */
- (void)setViewModel:(StatusViewModel *)viewModel {
    if (viewModel != nil) {
        _viewModel = viewModel;
        self.nameLabel.text = viewModel.status.user.screen_name;
        
        // 加载头像
        [self.iconView sd_setImageWithURL:viewModel.userProfileUrl placeholderImage:viewModel.userDefaultImage];
        
        //  设置认证图像
        self.vipIconView.image = viewModel.userVipImage;
        
        // 会员头像
        self.memberIconView.image = viewModel.userMemberImage;
        
        // TODO: 设置文字细节
        self.timeLabel.text = @"就是现在";
        self.sourceLabel.text = @"来自i 微博";
        
//        self.timeLabel.text = viewModel.status.created_at;
//        self.sourceLabel.text = viewModel.status.source;
    }
}

@end
